package com.qf.controller;


import com.qf.pojo.DtsStorage;
import com.qf.service.StorageService;
import com.qf.util.ResponseUtil;
import com.qf.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/storage")
public class AdminStorageController {


    @Autowired
    StorageService storageService;

    @RequestMapping("/create")
    public Object upFile(MultipartFile file) throws Exception {


        //原始文件名  1.jpg
        String filename = file.getOriginalFilename();
        //文件大小
        long size = file.getSize();
        //图片类型
        String type = file.getContentType();
        //获取唯一的名称
        String uuid = UUID.randomUUID().toString();
        String objectName = uuid + filename;


        DtsStorage storage = storageService.uploadFile(filename, (int) size, type, objectName, file.getBytes());
        return ResponseUtil.ok(storage);


    }
    @RequestMapping("/list")
    public  Object getList(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                           @RequestParam(value = "limit",required = false,defaultValue = "3") Integer limit,
                           @RequestParam(value = "sort",required = false,defaultValue = "add_time") String sort,
                           @RequestParam(value = "order",required = false,defaultValue = "desc") String order,String key,String name){

        Map<String,Object> map = storageService.findPage(page,limit,sort,order,key,name);

        return ResponseUtil.ok(map);
    }
    @RequestMapping("/delete")
    public Object deleteGoods(@RequestBody DtsStorage dtsStorage){

        storageService.deleteStorage(dtsStorage);
        return ResponseUtil.ok();

    }
}
